//package com.wejoy.video.middle.station.api.aspect;
//
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.TypeReference;
//import com.google.common.collect.Maps;
//import com.wejoy.video.middle.station.data.exception.BusinessException;
//import com.wejoy.video.middle.station.data.util.ApiRsaUtil;
//import com.wejoy.video.middle.station.data.util.RSAUtil;
//import com.wejoy.video.middle.station.data.util.StringUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.util.*;
//
//@Aspect
//@Component
//public class SignatureAuthAspect {
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//
//    private static final Map<String, String> appMap = new HashMap<String, String>() {{
//        put("8d55bb5797fc8ff6", "6249a40a2c1e6018b17993b0014be030");
//    }};
//    @Before(value = "@annotation(com.wejoy.video.middle.station.api.annotation.SignatureAuth)")
//    public void beforeControllerMethodWithSignatureAuth(JoinPoint joinPoint) throws Exception {
//        // 获取请求
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        // 获取请求参数的Map集合
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        Map<String, String> postRequestBody = getPostRequestBody(request);
//        Set<String> keySet = postRequestBody.keySet();
//        String[] keyArray = keySet.toArray(new String[keySet.size()]);
//        Arrays.sort(keyArray);  //排序
//        StringBuilder sb = new StringBuilder();
//        for (String k : keyArray) {
//            if (postRequestBody.get(k).trim().length() > 0) // 参数值为空，则不参与签名
//                sb.append(k).append("=").append(postRequestBody.get(k).trim()).append("&");
//        }
//        String nonce = request.getHeader("nonce");
//        String timestamp = request.getHeader("timestamp");
//        String sign = request.getHeader("sign");
//        String appId = request.getHeader("AppId");
//        //拼接appSecret
//        if(StringUtils.isEmpty(nonce)&&StringUtils.isEmpty(timestamp)&&StringUtils.isEmpty(sign)&&StringUtils.isEmpty(appId)){
//            throw new BusinessException("参数异常！");
//        }
//        //120s内为合法请求
//        if (Math.abs(Long.parseLong(timestamp) - System.currentTimeMillis()) > 120000) {
//            throw new BusinessException("timestamp失效");
//        }
//        sb.append("appSecret=").append(appMap.get(appId)).append("&timestamp=").append(timestamp).append("&nonce=").append(nonce);
//        System.out.println(sb);
//        String encrypt="";
//        String encrypt1 = ApiRsaUtil.encryptWithPrivateKey(sb.toString(),ApiRsaUtil.PRIVATE_KEY);
//        encrypt = Base64.getEncoder().encodeToString(encrypt1.getBytes());
//        System.out.println("encrypt1:"+encrypt1);
//        System.out.println("encrypt:"+encrypt);
//        if(!StringUtils.equals(encrypt,sign)){
//            throw new BusinessException("验签异常！");
//        }
//        if(!isReplayAttack(appId, timestamp, nonce, sign)){
//            throw new BusinessException("请勿重复提交");
//        }
//    }
//
//    public boolean isReplayAttack(String appId, String timeStamp, String nonce, String signature) {
//        StringBuilder redisKey = new StringBuilder();
//        redisKey.append("IS_REPLAY_ATTACK").append(":")
//                .append("APP_ID").append(":").append(appId)
//                .append("TIME_STAMP").append(":").append(timeStamp)
//                .append("NONCE").append(":").append(nonce)
//                .append("SIGN").append(":").append(signature);
//        Object value = redisTemplate.opsForValue().get(redisKey.toString());
//        if (value != null && StringUtils.equals(signature, value.toString().trim()))
//            return false;
//        else
//            redisTemplate.opsForValue().set(redisKey.toString(), signature.trim(), 1000 * 50);
//        return true;
//    }
//
//
//    private Map<String, String> getPostRequestBody(HttpServletRequest request) {
//        Map<String, String> paramMap = new HashMap<>();
//        BufferedReader reader = null;
//        try {
//            reader = request.getReader();
//            StringBuilder sb = new StringBuilder();
//            String line;
//            while ((line = reader.readLine()) != null) {
//                sb.append(line);
//            }
//            // 将请求体转换为Map，具体实现依赖您的参数格式
//            // 这里假设请求体是JSON格式的数据
//            paramMap = JSON.parseObject(sb.toString(), new TypeReference<Map<String, String>>(){});
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (reader != null) {
//                try {
//                    reader.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return paramMap;
//    }
//}
